<template>
  <div id="searchQuestionView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="题目名称">
        <a-input
          v-model="searchParams.title"
          placeholder="请输入搜索题目名称"
        />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-input-tag
          v-model="searchParams.tags"
          placeholder="请输入搜索标签"
          style="min-width: 200px"
        />
      </a-form-item>
      <a-form-item>
        <a-button @click="doSearch" type="primary">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #id="{ record }">
        {{ record.id.substring(record.id.length - 5) }}
      </template>
      <template #title="{ record }">
        {{ record.title }}
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="cyan"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        {{
          `${record.acceptedNum ? record.acceptedNum / record.submitNum : 0}%
          (${record.acceptedNum}/${record.submitNum})`
        }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doQuestion(record)">做题</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const router = useRouter();
const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  title: "",
  tags: [],
  pageSize: 10,
  current: 1,
} as QuestionQueryRequest);

const onPageChange = (pageNumber: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: pageNumber,
  };
};

const doQuestion = async (question: Question) => {
  // 点击做题之后需要跳转到做题页面
  await router.push({
    path: `/view/question/${question.id}`,
  });
};
const loadData = async () => {
  const result = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value
  );
  if (result.code === 0) {
    dataList.value = result.data.records;
    total.value = result.data.total;
  } else {
    message.error("加载失败，" + result.message);
  }
};

/**
 * watchEffect的作用：监听传入的方法里的所有的变量，一旦有一个值发生改变就会触发loadData()方法
 * 这里是监听 searchParams 变量，变量改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});
/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "题号",
    slotName: "id",
  },
  {
    title: "题目名称",
    slotName: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "acceptedRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    slotName: "optional",
  },
];

/**
 * 点击搜索触发事件
 */
const doSearch = () => {
  //为了防止当前页不为1时，搜索不到数据，每次执行该事件时，都重新给当前页赋值
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  // 重新根据搜索参数，加载数据
  //这里可以不用执行loadData方法，因为上面的 watchEffect已经监听了searchParams，searchParams发生改变就会执行loadData方法
  // loadData();
};
</script>

<style scoped>
#searchQuestionView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
